iT邦幫忙

2023 iThome 鐵人賽

DAY 12
0
自我挑戰組

自己的 Leak, 自己抓(swift)系列 第 12

再次面對 SourceKitten(CursorInfo)

  • 分享至 

  • xImage
  •  

再次面對 SourceKitten(CursorInfo)

在這個章節會使用 SourceKittenSourceKit 打交道。

以及唯一會使用的 api cursor info


用途

cursor info 主要的目的,就是詢問 目前游標所在位置資訊

例如我們將游標移到 Line19:Col5,於是就能取的 #expect 的資訊

https://ithelp.ithome.com.tw/upload/images/20230926/20158030U0bKxmMffY.png


使用 CursorInfo

需要的參數分別是:

  • 檔案所在位置
  • 目標在檔案的偏移量(不是 L19:C5)
    • func a() {}
    • a.offset is 5
  • 編譯檔案用的參數
enum Request {
    case cursorInfo(file: String, offset: ByteCount, arguments: [String])

    fileprivate var sourcekitObject: SourceKitObject {
        switch self {
        case let .cursorInfo(file, offset, arguments):
            return [
                "key.request": UID("source.request.cursorinfo"),
                "key.name": file,
                "key.sourcefile": file,
                "key.offset": Int64(offset.value),
                "key.compilerargs": arguments
            ]
        }
    }
}

然候我們可以透過 send() 指令取得 Response

public func send() throws -> [String: SourceKitRepresentable] {...}

public protocol SourceKitRepresentable {
    func isEqualTo(_ rhs: SourceKitRepresentable) -> Bool
}
extension Array: SourceKitRepresentable {}
extension Dictionary: SourceKitRepresentable {}
extension String: SourceKitRepresentable {}
extension Int64: SourceKitRepresentable {}
extension Bool: SourceKitRepresentable {}
extension Data: SourceKitRepresentable {}

如下:

"key.containertypeusr": "$sSo17OS_dispatch_queueCD"
"key.typename": "(DispatchQueue) -> (DispatchGroup?, DispatchQoS, DispatchWorkItemFlags, @escaping @convention(block) () -> ()) -> ()"
"key.typeusr": "$s5group3qos5flags7executeySo012OS_dispatch_A0CSg_8Dispatch0G3QoSVAH0G13WorkItemFlagsVyyXLtcD"
"key.related_decls":
- "key.annotated_decl": "<RelatedName usr=\"s:So17OS_dispatch_queueC8DispatchE5async7executeyAC0D8WorkItemC_tF\">async(execute:)</RelatedName>"
- "key.annotated_decl": "<RelatedName usr=\"s:So17OS_dispatch_queueC8DispatchE5async5group7executeySo0a1_b1_F0C_AC0D8WorkItemCtF\">async(group:execute:)</RelatedName>"
"key.usr": "s:So17OS_dispatch_queueC8DispatchE5async5group3qos5flags7executeySo0a1_b1_F0CSg_AC0D3QoSVAC0D13WorkItemFlagsVyyXLtF"
"key.annotated_decl": "<Declaration>func async(group: <Type usr=\"c:objc(cs)OS_dispatch_group\">DispatchGroup</Type>? = nil, qos: <Type usr=\"s:8Dispatch0A3QoSV\">DispatchQoS</Type> = .unspecified, flags: <Type usr=\"s:8Dispatch0A13WorkItemFlagsV\">DispatchWorkItemFlags</Type> = [], execute work: @escaping @convention(block) () -&gt; <Type usr=\"s:s4Voida\">Void</Type>)</Declaration>"
"key.is_dynamic": true
"key.is_system": true
"key.receivers":
- "key.usr": "c:objc(cs)OS_dispatch_queue"
"key.name": "async(group:qos:flags:execute:)"
"key.kind": "source.lang.swift.ref.function.method.instance"
"key.fully_annotated_decl": "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>async</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>group</decl.var.parameter.argument_label>: <decl.var.parameter.type><ref.class usr=\"c:objc(cs)OS_dispatch_group\">DispatchGroup</ref.class>?</decl.var.parameter.type> = nil</decl.var.parameter>, <decl.var.parameter><decl.var.parameter.argument_label>qos</decl.var.parameter.argument_label>: <decl.var.parameter.type><ref.struct usr=\"s:8Dispatch0A3QoSV\">DispatchQoS</ref.struct></decl.var.parameter.type> = .unspecified</decl.var.parameter>, <decl.var.parameter><decl.var.parameter.argument_label>flags</decl.var.parameter.argument_label>: <decl.var.parameter.type><ref.struct usr=\"s:8Dispatch0A13WorkItemFlagsV\">DispatchWorkItemFlags</ref.struct></decl.var.parameter.type> = []</decl.var.parameter>, <decl.var.parameter><decl.var.parameter.argument_label>execute</decl.var.parameter.argument_label> <decl.var.parameter.name>work</decl.var.parameter.name>: <syntaxtype.keyword>@escaping</syntaxtype.keyword> <decl.var.parameter.type><syntaxtype.attribute.builtin><syntaxtype.attribute.name>@convention</syntaxtype.attribute.name>(block)</syntaxtype.attribute.builtin> () -&gt; <decl.function.returntype><ref.typealias usr=\"s:s4Voida\">Void</ref.typealias></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>)</decl.function.method.instance>"
"key.decl_lang": "source.lang.swift"
"key.doc.full_as_xml": "<Function><Name>async(group:qos:flags:execute:)</Name><USR>s:So17OS_dispatch_queueC8DispatchE5async5group3qos5flags7executeySo0a1_b1_F0CSg_AC0D3QoSVAC0D13WorkItemFlagsVyyXLtF</USR><Declaration>func async(group: DispatchGroup? = nil, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], execute work: @escaping @convention(block) () -&gt; Void)</Declaration><CommentParts><Abstract><Para>Submits a work item to a dispatch queue and optionally associates it with a dispatch group. The dispatch group may be used to wait for the completion of the work items it references.</Para></Abstract><Parameters><Parameter><Name>group</Name><Direction isExplicit=\"0\">in</Direction><Discussion><Para>the dispatch group to associate with the submitted work item. If this is <codeVoice>nil</codeVoice>, the work item is not associated with a group.</Para></Discussion></Parameter><Parameter><Name>flags</Name><Direction isExplicit=\"0\">in</Direction><Discussion><Para>flags that control the execution environment of the</Para></Discussion></Parameter><Parameter><Name>qos</Name><Direction isExplicit=\"0\">in</Direction><Discussion><Para>the QoS at which the work item should be executed. Defaults to <codeVoice>DispatchQoS.unspecified</codeVoice>.</Para></Discussion></Parameter><Parameter><Name>flags</Name><Direction isExplicit=\"0\">in</Direction><Discussion><Para>flags that control the execution environment of the work item.</Para></Discussion></Parameter><Parameter><Name>execute</Name><Direction isExplicit=\"0\">in</Direction><Discussion><Para>The work item to be invoked on the queue.</Para></Discussion></Parameter></Parameters><Discussion><See><Para><codeVoice>sync(execute:)</codeVoice></Para></See><See><Para><codeVoice>DispatchQoS</codeVoice></Para></See><See><Para><codeVoice>DispatchWorkItemFlags</codeVoice></Para></See></Discussion></CommentParts></Function>"
"key.modulename": "Dispatch"

上一篇
掃瞄完 ID 之後~
下一篇
Ref 變數的來源(key.offset)
系列文
自己的 Leak, 自己抓(swift)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言